/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.regulacion.service;

import java.util.Date;
import java.util.List;

import co.gov.medellin.sdep.sisdep.common.message.MessageDescriptorConstant;
import co.gov.medellin.sdep.sisdep.common.service.BaseService;
import co.gov.medellin.sdep.sisdep.model.Carne;
import co.gov.medellin.sdep.sisdep.model.dao.impl.ICarneDAOT;
import co.gov.medellin.sdep.sisdep.regulacion.exception.CarneException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.CarneRuntimeException;
import co.gov.medellin.sdep.sisdep.regulacion.exception.descriptor.CarneMessageDescriptor;


/**
 * <p>
 * Implementacion de todos los servicios necesarios para la gestion de los Carne de los Comercientes en SISDEP.
 * </p>
 * <p>
 * Copyright (c) 2012<BR>
 * Consult-Soft S.A.<BR>
 * Created on: Apr 05, 2012<BR>
 * 
 * @author atabares
 * </p>
 */
public class CarneServiceImpl extends BaseService implements ICarneService {

	/** Serial key. */
	private static final long serialVersionUID = 4331894492796997982L;
	
	/** Interface de acceso a los DAOs de Carne. */
	private ICarneDAOT carneDAO;

	
	/** Constructor por defecto. */
	public CarneServiceImpl() {
	}

	/**
	* Obtiene el parametro carneDAO.
	* @see CarneServiceImpl#carneDAO
	*
	* @return El parametro carneDAO.
	*/
	public ICarneDAOT getCarneDAO() {
		return carneDAO;
	}

	/**
	* Asigna el parametro carneDAO.
	* @see CarneServiceImpl#carneDAO
	*
	* @param carneDAO valor a asignar al parametro carneDAO.
	*/
	public void setCarneDAO(ICarneDAOT carneDAO) {
		this.carneDAO = carneDAO;
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#insert(co.gov.medellin.sdep.sisdep.model.Carne)
	 */
	public void insert(Carne carne) throws CarneException {
		try{
			carne.setUsuarioCrea(getPrincipalUser());
			carne.setFechaCrea(new Date());
			
			getCarneDAO().insert(carne);
		} catch (Exception e) {
			Object [] infoError = {
				carne.getCodigo()
			};
			
			if (e.getCause().toString().indexOf("UNIQUE KEY") >= 0) {
				throw new CarneException(new CarneMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			} else {
				throw new CarneException(new CarneMessageDescriptor(MessageDescriptorConstant.SAVE), infoError, e);
			}
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#update(co.gov.medellin.sdep.sisdep.model.Carne)
	 */
	public final void update(Carne carne) throws CarneException {
		try{
			carne.setUsuarioModifica(getPrincipalUser());
			carne.setFechaModifica(new Date());
			
			getCarneDAO().update(carne);
		} catch (Exception e) {
			Object [] infoError = {
				carne.getCodigo()
			};
			
			if (e.getCause().toString().indexOf("UNIQUE KEY") >= 0) {
				throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.UK_VIOLATION), infoError, e);
			} else {
				throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.UPDATE), infoError, e);
			}
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#delete(co.gov.medellin.sdep.sisdep.model.Carne)
	 */
	public final void delete(Carne carne) throws CarneException {
		try {
			carne.setUsuarioModifica(getPrincipalUser());
			carne.setFechaModifica(new Date());
			
			getCarneDAO().delete(carne);
		} catch (Exception e) {
			Object [] infoError = {
				carne.getCodigo()
			};
			
			throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.DELETE), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#load(int, int, co.gov.medellin.sdep.sisdep.model.Carne)
	 */
	public final List load(int start, int max, Carne carne) throws CarneException {
		try{
			//return getCarneDAO().getAllCarnePaginated(start, max, criteria);
			return getCarneDAO().load();
		} catch (Exception e) {
			Object [] infoError = null;
			
			throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#get(long)
	 */
	public final Carne get(long id) throws CarneException {
		try {
			return getCarneDAO().findByPrimaryKey(id);
		} catch (Exception e) {
			Object [] infoError = {new Long(id)};
			
			throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.GET), infoError, e);
		}
	}
	
	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#getCarneListByCriteria(co.gov.medellin.sdep.sisdep.model.Carne)
	 */
	public final List getCarneListByCriteria(Carne carne) throws CarneException {
		try{
			return getCarneDAO().getCarneListByCriteria(carne);
		} catch (Exception e) {
			Object [] infoError = null;
			
			throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}

	/* (non-Javadoc)
	 * @see co.gov.medellin.sdep.sisdep.regulacion.service.ICarneService#deliveryCarne(java.lang.String)
	 */
	public void deliveryCarne(Carne carne) {
		try{
			getCarneDAO().deliveryCarne(carne);
		} catch (Exception e) {
			Object [] infoError = null;
			
			throw new CarneRuntimeException(new CarneMessageDescriptor(MessageDescriptorConstant.RETRIEVE), infoError, e);
		}
	}
}
